package com.bwie.auth.controller;

import com.bwie.auth.service.UserLoginService;
import com.bwie.common.constants.Constants;
import com.bwie.common.domain.TUser;
import com.bwie.common.domain.dto.ForgetPasswordDto;
import com.bwie.common.domain.dto.UserLoginRequest;
import com.bwie.common.domain.dto.UserRegisteredReq;
import com.bwie.common.domain.vo.JwtResponse;
import com.bwie.common.result.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户登录
 * @author zt181
 * @date 2025/7/15
 */

@RestController
@RequestMapping("/user")
public class UserLoginController {

    @Resource
    private UserLoginService userLoginService;

    /**
     * 用户登录
     * @param userLoginRequest 登录请求参数
     * @return jwt
     */
    @PostMapping("/login")
    public Result<JwtResponse> login(@RequestBody UserLoginRequest userLoginRequest) {
        Result<TUser> userInfo = userLoginService.login(userLoginRequest);
        if (!(Constants.SUCCESS == userInfo.getCode())) {
            return Result.error(userInfo.getMsg());
        }
        return userLoginService.createToken(userInfo);
    }


    /**
     * 用户注册
     * @param req 注册参数
     * @return 注册结果
     */
    @PostMapping("/registered")
    public Result registered(UserRegisteredReq req) {
        return userLoginService.registered(req);
    }

    /**
     * 发送短信验证码
     * @param mobile 手机号
     * @return 发送结果
     */
    @PostMapping("/sendSms")
    public Result sendSms(String mobile) {
        return userLoginService.sendSms(mobile);
    }

    @GetMapping("/send")
    public Result send(@PathVariable String mobile) {
        return userLoginService.send(mobile);
    }

    /**
     * 忘记密码
     * @return 忘记密码结果
     */
    @PostMapping("/forgetPassword")
    public Result forgetPassword(@RequestBody ForgetPasswordDto req) {
        return userLoginService.forgetPassword(req);
    }

}
